package demo_synchornized;


/**
 * @author: android 超级兵
 * @create: 2022-01-15 13:15
 * TODO synchronized的使用 count ++
 **/
public class Client2 {

    public static  int count;
    // 添加个数
    public static int countLength = 1000 * 100;

    public static class Thread1 extends Thread {
        @Override
        public void run() {
            super.run();
            addCount();
        }
    }

    private static final Object object = new Object();

    public static void addCount() {
        for (int i = 0; i < countLength; i++) {
//            synchronized (Client2.class) {
            synchronized (object) {
                count++;
            }
        }
    }

    public  synchronized void addCount2() {
        for (int i = 0; i < countLength; i++) {
            count++;
        }
    }

    public static class Thread2 extends Thread {
        @Override
        public void run() {
            super.run();
            addCount();
        }
    }

    public static void main(String[] args) {
        Thread1 thread1 = new Thread1();
        thread1.start();

        Thread2 thread2 = new Thread2();
        thread2.start();

        try {
            Thread.sleep(100);
            System.out.println("当前count个数为:" + count);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}